﻿using activeX;
using OpenClientX.Utils;
using Printer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace OpenClientX
{
    [Guid("65D8E97F-D3E2-462A-B389-241D7C38C518")]
    public class CommonApi : ActiveXControl
    {
        /// <summary>
        /// 选择条码打印机
        /// </summary>
        /// <returns></returns>
        public string SelectPrinter()
        {
            SelectPrinter sp = new SelectPrinter();
            sp.ShowDialog();
            return "";
        }

        /// <summary>
        /// 打印条码
        /// </summary>
        /// <param name="BarCode"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String PrintBarCode(String BarCode, String serviceUrl)
        {
            //先清除原来的条码图像缓存
            CommontUtils.clearCache();
            try
            {
                if (!String.IsNullOrEmpty(serviceUrl))
                {
                    if (CommontUtils.UpgradeTag(serviceUrl))
                    {
                        MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。", "提示信息", MessageBoxButtons.OK);
                        CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                        return "";
                    }
                }
                String fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                PrintDocument fPrintDocument = new PrintDocument();
                //MessageBox.Show(fPrintDocument.PrinterSettings.PrinterName);
                if (fPrintDocument.PrinterSettings.PrinterName.Contains("PC42t"))
                {
                    //MessageBox.Show("in PC42t");
                    BarCodeUtil.CreateBarcode(BarCode, fileName + ".bmp", true);
                    HoneywellPrintHelper.StartPrint("C://MssClientCache//barcodeCache//" + fileName + ".bmp", fPrintDocument.PrinterSettings.PrinterName);
                }
                else
                {
                    //MessageBox.Show("in Zebar");
                    BarCodeUtil.CreateBarcode(BarCode, fileName + ".png", false);
                    PrintService printService = new PrintService();
                    printService.StartPrint(new Bitmap("C://MssClientCache//barcodeCache//" + fileName + ".png"), "image");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "yes";
        }

        /// <summary>
        /// 启动客户端的接口
        /// </summary>
        /// <param name="username"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String openCLient(String username, String serviceUrl)
        {
            try
            {
                //MessageBox.Show("username:" + username + ",serviceUrl:" + serviceUrl);
                if (!String.IsNullOrEmpty(serviceUrl))
                {
                    if (CommontUtils.UpgradeTag(serviceUrl))
                    {
                        MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。", "提示信息", MessageBoxButtons.OK);
                        CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                        return "";
                    }
                }

                if (!File.Exists("C:\\MssClient\\MSSClient.exe"))
                {
                    return "noFile";
                }
                string str1 = username + ",NoPassWord";
                File.WriteAllText(@"C://openClient.txt", str1);
                Process p = Process.Start("C:\\MssClient\\MSSClient.exe");
            }
            catch (Exception ex)
            {
                CommontUtils.WriteLog(ex);
            }
            return "";
        }

        /// <summary>
        /// 启动客户端，进行补单获取
        /// </summary>
        /// <param name="username"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String getBudanInfo(String username, String serviceUrl)
        {
            if (!String.IsNullOrEmpty(serviceUrl))
            {
                if (CommontUtils.UpgradeTag(serviceUrl))
                {
                    MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。", "提示信息", MessageBoxButtons.OK);
                    CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                    return "";
                }
            }
            if (!File.Exists("C:\\MssClient\\MSSClient.exe"))
                return "noExeFile";

            string strOenCLient = username + ",NoPassWord";
            File.WriteAllText(@"C://MssClientCache//BudanInfoGet.txt", " ");
            File.WriteAllText(@"C://openClient.txt", strOenCLient);
            Process p = Process.Start("C:\\MssClient\\MSSClient.exe");
            return "";
        }
    }
}
